package com.qs.eggs.view.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.MainActivity;
import com.qs.eggs.R;
import com.qs.eggs.adapter.Address1Adapter;
import com.qs.eggs.adapter.AddressAdapter;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.refresh.utils.TDevice;
import com.qs.eggs.widgets.custom.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class AddressManagement1Activity extends Activity implements View.OnClickListener,PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener{
    private PullToRefreshView mPullToRefreshView;
    private ImageView fanhui;
    private TextView tv_add;
    private  RecyclerView recycleView;
    private  Button add;
    private Address1Adapter addressAdapter;
    private List<Map<String, Object>> addlist= null;
    public static Activity instance;
   // private String address;
    private String isdefault,id,province,city,area,village,custname,phone,address,provinceid,cityid,areaid,villageid;
    private Handler mHandler;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance=this;
        setContentView(R.layout.activity_address_management1);
        initView();
        getAddress();
    }
    private void initView(){
        fanhui=(ImageView)findViewById(R.id.fanhui);
        tv_add=(TextView)findViewById(R.id.tv_add);
        recycleView=(RecyclerView)findViewById(R.id.recycleView);
        add=(Button)findViewById(R.id.add);
        mPullToRefreshView =(PullToRefreshView) findViewById(R.id.main_pull_refresh_view);
        fanhui.setOnClickListener(this);
        tv_add.setOnClickListener(this);
        add.setOnClickListener(this);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
   }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui:
           AddressManagement1Activity.this.finish();
             break;
            case R.id.tv_add:
                Intent intent=new Intent(this,AddAddress1Activity.class);
                startActivity(intent);
                break;
            case R.id.add:
                Intent intent1=new Intent(this,AddAddress1Activity.class);
                startActivity(intent1);
                break;
     }
        }
    //获得地址信息
    public void getAddress(){
        OkGo.post(Constants.URL_BASE+"myorder?action=searchAddress")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("地址查询=="+response);
                        try {
                            addlist=new ArrayList<>();
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    Map<String, Object> map = new HashMap<>();
                                    map.put("custname", jsonObject.getString("custname"));
                                    map.put("phone", jsonObject.getString("phone"));
                                    map.put("address", jsonObject.getString("address"));
                                    map.put("custid", jsonObject.getString("custid"));
                                    map.put("isdefault", jsonObject.getString("isdefault"));
                                    map.put("id", jsonObject.getString("id"));
                                    map.put("province", jsonObject.getString("province"));
                                    map.put("city", jsonObject.getString("city"));
                                    map.put("area", jsonObject.getString("area"));
                                    map.put("village", jsonObject.getString("village"));
                                    map.put("provinceid", jsonObject.getString("provinceid"));
                                    map.put("cityid", jsonObject.getString("cityid"));
                                    map.put("areaid", jsonObject.getString("areaid"));
                                    map.put("villageid", jsonObject.getString("villageid"));
                                    addlist.add(map);

                            }

                            addressAdapter = new Address1Adapter(AddressManagement1Activity.this, addlist,mHandler);
                            recycleView.setAdapter(addressAdapter);
                            mHandler = new Handler(){
                                public void handleMessage(android.os.Message msg) {
                                    switch (msg.what) {
                                        case 5:
                                            getAddress();
                                            addressAdapter.notifyDataSetChanged();
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            };
                                addressAdapter.setOnItemClickListener(new Address1Adapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(View view, int position) {
                                        address = addlist.get(position).get("address").toString();
                                        provinceid=addlist.get(position).get("provinceid").toString();
                                        cityid=addlist.get(position).get("cityid").toString();
                                        areaid=addlist.get(position).get("areaid").toString();
                                        villageid=addlist.get(position).get("villageid").toString();
                                        Intent intent = new Intent(AddressManagement1Activity.this, SubmitOrderActivity.class);
                                        intent.putExtra("address", address);
                                        intent.putExtra("provinceid", provinceid);
                                        intent.putExtra("cityid", cityid);
                                        intent.putExtra("areaid", areaid);
                                        intent.putExtra("villageid", villageid);
                                        setResult(99, intent);
                                        finish();
                                    }
                                });

                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }

                });

 }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);
               getAddress();
                mPullToRefreshView.onFooterRefreshComplete();
            }
        }, 1000);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);
                getAddress();
                mPullToRefreshView.onHeaderRefreshComplete();
            }
        }, 1000);

    }
	/*-----------刷新接口方法实现-------	*/

    Handler handler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            mPullToRefreshView.onFooterRefreshComplete();

        }

        ;
    };
    @Override
    public void onResume(){
        super.onResume();
        getAddress();
    }


}
